<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Model\{Productattr}; 

class Product extends Model
{
	protected $table = 'product';
	protected $primaryKey = 'id';
	public $timestamps = false;
	
	public static function add_attr($product_id,$type_id,$attr_ids,$vals){
		$alls = Productattr::get_attr(['type_id'=>$type_id]);
		$alls = arrays_set_key($alls,'id');
		foreach($vals as $key=>$val){
			if(!empty($val)){
				$bool = DB::table('product_attr_value')->insert([
					'attr_id'=>$attr_ids[$key],
					'product_id'=>$product_id,
					'name'=>$alls[$attr_ids[$key]]->name,
					'value'=>$val,
				]);
			}
		}
		return true;
	}

	public static function updat_attr($attr_id,$val,$ids,$product_id){
		$product_attrs = SELF::get_product_attr($product_id);
		#拿出新增
		$adds = [];
		$post_ids = [];
		$updates = [];
		foreach($val as $key=>$value){
			if(empty($ids[$key])){
				$adds[] = [
					'attr_id'=>$attr_id[$key],
					'value'=>$value,
					'name'=>'',
					'product_id'=>$product_id,
				];
			}
			if(!empty($ids[$key])){
				$post_ids[] = $ids[$key];
				$updates[$ids[$key]] = $value;
			}
		}
		$original_ids = get_multiple_column($product_attrs,'id');
		
		foreach($product_attrs as $item){
			if(in_array($item->id,$post_ids)){
				DB::table('product_attr_value')->where('id',$item->id)->update([
						'value'=>$updates[$item->id],
					]);
			}else{
				
				DB::table('product_attr_value')->where('id',$item->id)->delete();
			}
		}


		$bool = DB::table('product_attr_value')->insert($adds);
		return true;
	}

	public static function get_product_attr($product_id){
		$db = DB::table('product_attr_value')->where('product_id',$product_id);
		$all = $db->get()->toArray();
		$rs = [];
		foreach($all as $r){
			$rs[$r->id] = $r;
		}
		return $rs;
	}

	/*
	 * $p['cat_id']
	 * $p['limit']
	 */
	public static function web_product_list($p){
		$field = ['id','cat_id','title','img','price'];
		$db = DB::table('product')->select($field);
		if(!empty($p['cat_id'])){
			$db->whereIn('cat_id',$p['cat_id']);
		}
		$db->orderBy('pos','desc')->limit($p['limit']);
		$data = $db->get()->toArray();
		foreach ($data as &$item) {
		 	$item->imgs = explode('|',$item->img);
		 	$item->img = $item->imgs[0];
		 } 
		return $data;
	}

	public static function view($id){
		$data = DB::table('product')->where('id',$id)->first();
		$data->imgs = explode('|',$data->img);
		$data->img = $data->imgs[0];
		$attr = Productattr::web_product_attr(['product_id'=>$id]);
		$data->attr = $attr;
		return $data;
	}
	/**
	 * attr_ids 提交购物车验证用户提交的属性合法
	 * $id 商品id
	 * 用户提交的数组
	 */
	public static function validate_attr($attr_ids=[],$id){
		$data = DB::table('product_attr_value')->where('product_id',$id)->get()->toArray();
		$ids = [];
		$attr_keys = [];
		foreach($data as $item){
			$ids[] = $item->id;
			$attr_keys[$item->attr_id] = $item->attr_id;
		}
		if( count($attr_keys)!= count($attr_ids) ){
			return false;
		}
		foreach($attr_ids as $id){
			if(!in_array($id,$ids)){
				return false;
			}
		}
		return true;
	}
}